{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "请选择题目编号（1-20）运行相应程序，或输入'q'退出:\n",
      "2025-03-23 是这一年的第 82 天\n",
      "\n",
      "请选择题目编号（1-20）运行相应程序，或输入'q'退出:\n",
      "1*1=1\t\n",
      "1*2=2\t2*2=4\t\n",
      "1*3=3\t2*3=6\t3*3=9\t\n",
      "1*4=4\t2*4=8\t3*4=12\t4*4=16\t\n",
      "1*5=5\t2*5=10\t3*5=15\t4*5=20\t5*5=25\t\n",
      "1*6=6\t2*6=12\t3*6=18\t4*6=24\t5*6=30\t6*6=36\t\n",
      "1*7=7\t2*7=14\t3*7=21\t4*7=28\t5*7=35\t6*7=42\t7*7=49\t\n",
      "1*8=8\t2*8=16\t3*8=24\t4*8=32\t5*8=40\t6*8=48\t7*8=56\t8*8=64\t\n",
      "1*9=9\t2*9=18\t3*9=27\t4*9=36\t5*9=45\t6*9=54\t7*9=63\t8*9=72\t9*9=81\t\n",
      "\n",
      "请选择题目编号（1-20）运行相应程序，或输入'q'退出:\n",
      "1000中有 168 个素数，素数分别是: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997]\n",
      "\n",
      "请选择题目编号（1-20）运行相应程序，或输入'q'退出:\n",
      "2*2=\n",
      "2*2\n",
      "\n",
      "请选择题目编号（1-20）运行相应程序，或输入'q'退出:\n",
      "A\n",
      "\n",
      "请选择题目编号（1-20）运行相应程序，或输入'q'退出:\n",
      "5 是 1 位数\n",
      "逆序打印出各位数字: 5\n",
      "\n",
      "请选择题目编号（1-20）运行相应程序，或输入'q'退出:\n",
      "英文字母: 4, 空格: 0, 数字: 0, 其它字符: 0\n",
      "\n",
      "请选择题目编号（1-20）运行相应程序，或输入'q'退出:\n"
     ]
    },
    {
     "ename": "ValueError",
     "evalue": "invalid literal for int() with base 10: 'a'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[5], line 318\u001b[0m\n\u001b[0;32m    316\u001b[0m \u001b[38;5;66;03m# 运行主程序\u001b[39;00m\n\u001b[0;32m    317\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;18m__name__\u001b[39m \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m__main__\u001b[39m\u001b[38;5;124m\"\u001b[39m:\n\u001b[1;32m--> 318\u001b[0m     \u001b[43mmain\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n",
      "Cell \u001b[1;32mIn[5], line 312\u001b[0m, in \u001b[0;36mmain\u001b[1;34m()\u001b[0m\n\u001b[0;32m    310\u001b[0m     \u001b[38;5;28;01mbreak\u001b[39;00m\n\u001b[0;32m    311\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m choice \u001b[38;5;129;01min\u001b[39;00m functions:\n\u001b[1;32m--> 312\u001b[0m     \u001b[43mfunctions\u001b[49m\u001b[43m[\u001b[49m\u001b[43mchoice\u001b[49m\u001b[43m]\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    313\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m    314\u001b[0m     \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m无效的题目编号，请重新输入。\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
      "Cell \u001b[1;32mIn[5], line 84\u001b[0m, in \u001b[0;36mquestion8\u001b[1;34m()\u001b[0m\n\u001b[0;32m     82\u001b[0m         total \u001b[38;5;241m+\u001b[39m\u001b[38;5;241m=\u001b[39m \u001b[38;5;28mint\u001b[39m(num_str)\n\u001b[0;32m     83\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m total\n\u001b[1;32m---> 84\u001b[0m a \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mint\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m请输入数字a: \u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m     85\u001b[0m n \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mint\u001b[39m(\u001b[38;5;28minput\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m请输入要累加的次数n: \u001b[39m\u001b[38;5;124m\"\u001b[39m))\n\u001b[0;32m     86\u001b[0m result \u001b[38;5;241m=\u001b[39m sum_of_repeated_numbers(a, n)\n",
      "\u001b[1;31mValueError\u001b[0m: invalid literal for int() with base 10: 'a'"
     ]
    }
   ],
   "source": [
    "# 导入必要的库\n",
    "import datetime\n",
    "import math\n",
    "\n",
    "# 题目1：输入某年某月某日，判断这一天是这一年的第几天？\n",
    "def question1():\n",
    "    year = int(input(\"请输入年份: \"))\n",
    "    month = int(input(\"请输入月份: \"))\n",
    "    day = int(input(\"请输入日期: \"))\n",
    "    date = datetime.date(year, month, day)\n",
    "    print(f\"{date} 是这一年的第 {date.timetuple().tm_yday} 天\")\n",
    "\n",
    "# 题目2：输出9x9乘法口诀表\n",
    "def question2():\n",
    "    for i in range(1, 10):\n",
    "        for j in range(1, i + 1):\n",
    "            print(f\"{j}*{i}={i * j}\", end=\"\\t\")\n",
    "        print()\n",
    "\n",
    "# 题目3：判断1000中有多少个素数，并输出所有素数\n",
    "def question3():\n",
    "    def is_prime(num):\n",
    "        if num < 2:\n",
    "            return False\n",
    "        for i in range(2, int(math.sqrt(num)) + 1):\n",
    "            if num % i == 0:\n",
    "                return False\n",
    "        return True\n",
    "    \n",
    "    primes = [num for num in range(2, 1001) if is_prime(num)]\n",
    "    print(f\"1000中有 {len(primes)} 个素数，素数分别是: {primes}\")\n",
    "    \n",
    "# 题目4：输入一个任意正整数，输出分解质因数\n",
    "def question4():\n",
    "    num = int(input(\"请输入一个正整数: \"))\n",
    "    factors = []\n",
    "    i = 2\n",
    "    while i * i <= num:\n",
    "        while (num % i) == 0:\n",
    "            factors.append(i)\n",
    "            num //= i\n",
    "        i += 1\n",
    "    if num > 1:\n",
    "        factors.append(num)\n",
    "    print(f\"{factors[0]}\", end=\"\")\n",
    "    for factor in factors[1:]:\n",
    "        print(f\"*{factor}\", end=\"\")\n",
    "    print(\"=\")\n",
    "    print(\"*\".join(map(str, factors)))\n",
    "\n",
    "# 题目5：利用条件运算符的嵌套来完成此题：学习成绩>=90分的同学用A表示，60-89分之间的用B表示，60分以下的用C表示\n",
    "def question5():\n",
    "    score = float(input(\"请输入学习成绩: \"))\n",
    "    grade = 'A' if score >= 90 else ('B' if 60 <= score < 90 else 'C')\n",
    "    print(grade)\n",
    "\n",
    "# 题目6：输入一个不多于6位的正整数，要求：一、求它是几位数，二、逆序打印出各位数字\n",
    "def question6():\n",
    "    num = int(input(\"请输入一个不多于6位的正整数: \"))\n",
    "    num_str = str(num)\n",
    "    digit_count = len(num_str)\n",
    "    reversed_num_str = num_str[::-1]\n",
    "    print(f\"{num} 是 {digit_count} 位数\")\n",
    "    print(f\"逆序打印出各位数字: {reversed_num_str}\")\n",
    "\n",
    "# 题目7：输入一行字符串，分别统计出其中英文字母、空格、数字和其它字符的个数\n",
    "def question7():\n",
    "    text = input(\"请输入一行字符串: \")\n",
    "    letters = sum(1 for char in text if char.isalpha())\n",
    "    spaces = text.count(\" \")\n",
    "    digits = sum(1 for char in text if char.isdigit())\n",
    "    others = len(text) - letters - spaces - digits\n",
    "    print(f\"英文字母: {letters}, 空格: {spaces}, 数字: {digits}, 其它字符: {others}\")\n",
    "\n",
    "# 题目8求s=a+aa+aaa+aaaa+aa...a的值，其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加)，几个数相加有键盘控制\n",
    "def question8():\n",
    "    def sum_of_repeated_numbers(a, n):\n",
    "        total = 0\n",
    "        num_str = \"\"\n",
    "        for i in range(n):\n",
    "            num_str += str(a)\n",
    "            total += int(num_str)\n",
    "        return total\n",
    "    a = int(input(\"请输入数字a: \"))\n",
    "    n = int(input(\"请输入要累加的次数n: \"))\n",
    "    result = sum_of_repeated_numbers(a, n)\n",
    "    print(f\"结果是: {result}\")\n",
    "\n",
    "# 题目9一个数如果恰好等于它的因子之和，这个数就称为“完数”。例如6=1＋2＋3.编程，找出1000以内的所有完数。\n",
    "def question9():\n",
    "    def find_factors(n):\n",
    "        \"\"\"返回n的所有真因子\"\"\"\n",
    "        factors = []\n",
    "        for i in range(1, n):\n",
    "            if n % i == 0:\n",
    "                factors.append(i)\n",
    "        return factors\n",
    "    def is_perfect_number(n):\n",
    "        \"\"\"判断一个数是否为完数\"\"\"\n",
    "        return sum(find_factors(n)) == n\n",
    "    def find_perfect_numbers(limit):\n",
    "        \"\"\"找出指定范围内的所有完数\"\"\"\n",
    "        perfect_numbers = []\n",
    "        for num in range(2, limit + 1):\n",
    "            if is_perfect_number(num):\n",
    "                perfect_numbers.append(num)\n",
    "        return perfect_numbers\n",
    "    perfect_numbers = find_perfect_numbers(1000)\n",
    "    print(\"1000以内的完数有：\", perfect_numbers)\n",
    "\n",
    "# 题目10有一分数序列：2/1，3/2，5/3，8/5，13/8，21/13...求出这个数列的前N项之和，N由键盘输入\n",
    "def question10():\n",
    "    def fibonacci(n):\n",
    "        fib = [0, 1]\n",
    "        for i in range(2, n + 1):\n",
    "            fib.append(fib[-1] + fib[-2])\n",
    "        return fib\n",
    "\n",
    "    def sum_of_series(N):\n",
    "        fib = fibonacci(N + 1)\n",
    "        total_sum = 0\n",
    "        for i in range(1, N + 1):\n",
    "            total_sum += fib[i + 1] / fib[i]\n",
    "        return total_sum\n",
    "    N = int(input(\"请输入N的值: \"))\n",
    "    result = sum_of_series(N)\n",
    "    print(f\"该数列的前{N}项之和为: {result}\")\n",
    "# 题目11输入任意数N，求1+2!+3!+...+N!的值（注意实数范围）\n",
    "def question11():\n",
    "    import math\n",
    "    def factorial(n):\n",
    "        \"\"\"计算n的阶乘\"\"\"\n",
    "        if n == 0 or n == 1:\n",
    "            return 1\n",
    "        else:\n",
    "            return math.gamma(n + 1)  # 使用伽玛函数来计算阶乘\n",
    "    def sum_of_factorials(N):\n",
    "        \"\"\"计算1 + 2! + 3! + ... + N!的值\"\"\"\n",
    "        total_sum = 0\n",
    "        for i in range(1, int(math.floor(N)) + 1):\n",
    "            total_sum += factorial(i)\n",
    "        return total_sum\n",
    "    N = float(input(\"请输入一个实数N: \"))\n",
    "    result = sum_of_factorials(N)\n",
    "    print(f\"1 + 2! + 3! + ... + {int(math.floor(N))}! 的值是: {result}\")\n",
    "\n",
    "# 题目12一个整数，它加上100后是一个完全平方数，再加上268又是一个完全平方数，请问该数是多少？\n",
    "def question12():\n",
    "    def is_perfect_square(n):\n",
    "        root = int(n ** 0.5)\n",
    "        return root * root == n\n",
    "    for x in range(10**6):  # 假设整数不会太大，这里取一个合理的上限\n",
    "        if is_perfect_square(x + 100) and is_perfect_square(x + 100 + 268):\n",
    "            print(f\"满足条件的整数是: {x}\")\n",
    "            break  # 找到答案后退出循环\n",
    "\"\"\"题目13输入任意奇数N，按如下要求打印出图案（菱形）\n",
    "    *\n",
    "  ***\n",
    " *****\n",
    "*******（N控制行数和本行的星数）\n",
    " *****\n",
    "  ***\n",
    "   *\n",
    "   \"\"\"\n",
    "def question13():\n",
    "    def print_diamond(N):\n",
    "        if N % 2 == 0:\n",
    "            print(\"请输入一个奇数\")\n",
    "            return\n",
    "        for i in range(1, N + 1, 2):\n",
    "            spaces = (N - i) // 2\n",
    "            print(' ' * spaces + '*' * i + ' ' * spaces)\n",
    "        for i in range(N - 2, 0, -2):\n",
    "            spaces = (N - i) // 2\n",
    "            print(' ' * spaces + '*' * i + ' ' * spaces)\n",
    "    N = int(input(\"请输入一个奇数N: \"))\n",
    "    print_diamond(N)\n",
    "\n",
    "# 题目14有一对兔子，从出生后第3个月起每个月都生一对兔子，小兔子长到第三个月后每个月又生一对兔子，假如兔子都不死，问每个月的兔子总数为多少？\n",
    "def question14():\n",
    "    def rabbit_count(months):\n",
    "        if months == 1 or months == 2 or months == 3:\n",
    "            return 1\n",
    "        else:\n",
    "            a, b = 1, 1\n",
    "            for i in range(4, months + 1):\n",
    "                a, b = b, a + b\n",
    "            return b\n",
    "    for month in range(1, 13):\n",
    "        print(f\"第{month}个月的兔子总数为：{rabbit_count(month)}\")\n",
    "\n",
    "# 题目15对10个数进行排序。\n",
    "def question15():\n",
    "    def bubble_sort(arr):\n",
    "        n = len(arr)\n",
    "        for i in range(n):\n",
    "            for j in range(0, n-i-1):\n",
    "                if arr[j] > arr[j+1]:\n",
    "                    arr[j], arr[j+1] = arr[j+1], arr[j]\n",
    "        return arr\n",
    "    # 示例\n",
    "    numbers = [45,52, 75, 367, 25, 74, 33, 8, 81, 39]\n",
    "    sorted_numbers = bubble_sort(numbers)\n",
    "    print(\"排序后的数组:\", sorted_numbers)\n",
    "\n",
    "\n",
    "# 题目16任意输入一个3x3矩阵，求矩阵对角线元素之和。\n",
    "def question16():\n",
    "    import numpy as np\n",
    "\n",
    "    matrix = np.array([[2, 2, 3],\n",
    "                    [4, 5, 6],\n",
    "                    [7, 7, 9]])\n",
    "# 获取对角线元素\n",
    "    diagonal_elements = np.diagonal(matrix)\n",
    "\n",
    "# 计算对角线元素之和\n",
    "    sum_of_diagonal = np.sum(diagonal_elements)\n",
    "\n",
    "    print(\"对角线元素之和为:\", sum_of_diagonal)\n",
    "\n",
    "# 题目17有一个已经排好序的数组（元素数量大于10）。从键盘任意输入一个数，要求按原来的规律将它插入数组中。\n",
    "def question17():\n",
    "    def insert_into_sorted_array(arr, num):\n",
    "        left, right = 0, len(arr) - 1\n",
    "        while left <= right:\n",
    "            mid = (left + right) // 2\n",
    "            if arr[mid] < num:\n",
    "                left = mid + 1\n",
    "            else:\n",
    "                right = mid - 1\n",
    "        arr.insert(left, num)\n",
    "        return arr\n",
    "    sorted_array = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]\n",
    "    num = float(input(\"请输入一个数: \"))\n",
    "    new_array = insert_into_sorted_array(sorted_array, num)\n",
    "    print(\"插入后的数组是:\")\n",
    "    for element in new_array:\n",
    "        print(element)\n",
    "\n",
    "# 题目18有n个整数，使其前面各数顺序向后移m个位置，最后m个数变成最前面的m个数\n",
    "def question18():\n",
    "    def rotate_list(lst, n, m):\n",
    "        if n > len(lst) or m > len(lst):\n",
    "            return \"n或m超出列表长度\"\n",
    "        result = lst[-m:] + lst[:-m]\n",
    "        return result\n",
    "    example_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n",
    "    rotated_list = rotate_list(example_list, 10, 3)\n",
    "    print(rotated_list)  # 输出: [8, 9, 10, 1, 2, 3, 4, 5, 6, 7]\n",
    "\n",
    "# 题目19有n个人围成一圈，顺序排号。从第一个人开始报数（从1到3报数），凡报到3的人退出圈子，问最后留下的是原来第几号的那位。\n",
    "def question19():\n",
    "    def josephus(n, k):\n",
    "        if n == 1:\n",
    "            return 0\n",
    "        else:\n",
    "            return (josephus(n - 1, k) + k) % n\n",
    "    n = int(input(\"请输入总人数n: \"))\n",
    "    k = 3  # 从1到3报数，即k=3\n",
    "    last_person = josephus(n, k) + 1  # 加1是因为返回的是索引，而题目中编号是从1开始的\n",
    "    print(f\"最后留下的是原来第{last_person}号的那位\")\n",
    "\n",
    "# 题目20编写一个函数，输入n为偶数时，调用函数求1/2+1/4+...+1/n,当输入n为奇数时，调用函数1/1+1/3+...+1/n(利用指针函数)\n",
    "def question20():\n",
    "    def sum_even_series(n):\n",
    "        return sum(1 / i for i in range(2, n + 1, 2))\n",
    "    def sum_odd_series(n):\n",
    "        return sum(1 / i for i in range(1, n + 1, 2))\n",
    "    def calculate_series(n):\n",
    "        if n % 2 == 0:\n",
    "            series_sum_func = sum_even_series\n",
    "        else:\n",
    "            series_sum_func = sum_odd_series\n",
    "        return series_sum_func(n)\n",
    "    if __name__ == \"__main__\":\n",
    "        n = int(input(\"请输入一个整数n: \"))\n",
    "        result = calculate_series(n)\n",
    "        print(f\"结果是: {result}\")\n",
    "\n",
    "# 定义一个函数列表，将题目函数与题目编号关联\n",
    "functions = {\n",
    "    \"1\": question1,\n",
    "    \"2\": question2,\n",
    "    \"3\": question3,\n",
    "    \"4\": question4,\n",
    "    \"5\": question5,\n",
    "    \"6\": question6,\n",
    "    \"7\": question7, \n",
    "    \"8\": question8,\n",
    "    \"9\": question9,\n",
    "    \"10\": question10,\n",
    "    \"11\": question11,\n",
    "    \"12\": question12,\n",
    "    \"13\": question13,\n",
    "    \"14\": question14,\n",
    "    \"15\": question15,\n",
    "    \"16\": question16,\n",
    "    \"17\": question17,\n",
    "    \"18\": question18,\n",
    "    \"19\": question19,\n",
    "    \"20\": question20\n",
    "}\n",
    "# 交互选择结构\n",
    "def main():\n",
    "    while True:\n",
    "        print(\"\\n请选择题目编号（1-20）运行相应程序，或输入'q'退出:\")\n",
    "        choice = input(\"输入题目编号: \")\n",
    "        if choice.lower() == 'q':\n",
    "            break\n",
    "        if choice in functions:\n",
    "            functions[choice]()\n",
    "        else:\n",
    "            print(\"无效的题目编号，请重新输入。\")\n",
    "\n",
    "# 运行主程序\n",
    "if __name__ == \"__main__\":\n",
    "    main()\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
